/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl;

import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Condition;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.util.InteractionprotocolsValidator;

import java.util.Collection;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.EObjectValidator;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Interaction Path</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionPathImpl#getElement <em>Element</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionPathImpl#getGuard <em>Guard</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionPathImpl#getName <em>Name</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionPathImpl#getInteractionPathBusinessMessages <em>Interaction Path Business Messages</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class InteractionPathImpl extends EObjectImpl implements InteractionPath {
	/**
	 * The cached value of the '{@link #getElement() <em>Element</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getElement()
	 * @generated
	 * @ordered
	 */
	protected EList<InteractionProtocolElement> element;

	/**
	 * The cached value of the '{@link #getGuard() <em>Guard</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getGuard()
	 * @generated
	 * @ordered
	 */
	protected Condition guard;

	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The cached value of the '{@link #getInteractionPathBusinessMessages() <em>Interaction Path Business Messages</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getInteractionPathBusinessMessages()
	 * @generated
	 * @ordered
	 */
	protected EList<BusinessMessage> interactionPathBusinessMessages;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected InteractionPathImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return InteractionprotocolsPackage.Literals.INTERACTION_PATH;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InteractionProtocolElement> getElement() {
		if (element == null) {
			element = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT);
		}
		return element;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Condition getGuard() {
		return guard;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetGuard(Condition newGuard, NotificationChain msgs) {
		Condition oldGuard = guard;
		guard = newGuard;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PATH__GUARD, oldGuard, newGuard);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setGuard(Condition newGuard) {
		if (newGuard != guard) {
			NotificationChain msgs = null;
			if (guard != null)
				msgs = ((InternalEObject)guard).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - InteractionprotocolsPackage.INTERACTION_PATH__GUARD, null, msgs);
			if (newGuard != null)
				msgs = ((InternalEObject)newGuard).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - InteractionprotocolsPackage.INTERACTION_PATH__GUARD, null, msgs);
			msgs = basicSetGuard(newGuard, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PATH__GUARD, newGuard, newGuard));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PATH__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<BusinessMessage> getInteractionPathBusinessMessages() {
		if (interactionPathBusinessMessages == null) {
			interactionPathBusinessMessages = new EObjectResolvingEList<BusinessMessage>(BusinessMessage.class, this, InteractionprotocolsPackage.INTERACTION_PATH__INTERACTION_PATH_BUSINESS_MESSAGES);
		}
		return interactionPathBusinessMessages;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT:
				return ((InternalEList<?>)getElement()).basicRemove(otherEnd, msgs);
			case InteractionprotocolsPackage.INTERACTION_PATH__GUARD:
				return basicSetGuard(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT:
				return getElement();
			case InteractionprotocolsPackage.INTERACTION_PATH__GUARD:
				return getGuard();
			case InteractionprotocolsPackage.INTERACTION_PATH__NAME:
				return getName();
			case InteractionprotocolsPackage.INTERACTION_PATH__INTERACTION_PATH_BUSINESS_MESSAGES:
				return getInteractionPathBusinessMessages();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT:
				getElement().clear();
				getElement().addAll((Collection<? extends InteractionProtocolElement>)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__GUARD:
				setGuard((Condition)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__NAME:
				setName((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__INTERACTION_PATH_BUSINESS_MESSAGES:
				getInteractionPathBusinessMessages().clear();
				getInteractionPathBusinessMessages().addAll((Collection<? extends BusinessMessage>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT:
				getElement().clear();
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__GUARD:
				setGuard((Condition)null);
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__NAME:
				setName(NAME_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PATH__INTERACTION_PATH_BUSINESS_MESSAGES:
				getInteractionPathBusinessMessages().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PATH__ELEMENT:
				return element != null && !element.isEmpty();
			case InteractionprotocolsPackage.INTERACTION_PATH__GUARD:
				return guard != null;
			case InteractionprotocolsPackage.INTERACTION_PATH__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case InteractionprotocolsPackage.INTERACTION_PATH__INTERACTION_PATH_BUSINESS_MESSAGES:
				return interactionPathBusinessMessages != null && !interactionPathBusinessMessages.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(')');
		return result.toString();
	}

	public EList<InteractionProtocolElement> getOrderedIpElement() {
//		if (element == null) {
//			element = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT);
//			return element;
//		}
		
		EList<InteractionProtocolElement> ipElement = getRealIpElement();
		EList<InteractionProtocolElement> orderedIpElement =  new BasicEList<InteractionProtocolElement>();
		
		if (ipElement.size()==0)
			return orderedIpElement;

		InteractionProtocolElement middleElement = ipElement.get(0);
		if (middleElement==null)
			return orderedIpElement;
		
		InteractionProtocolElement actualElement = middleElement;
		orderedIpElement.add(actualElement);
		for (int i=1;i<ipElement.size();i++){
			actualElement = getPredecesor(actualElement, ipElement);
			
			if (actualElement==null)
				break;
			
			orderedIpElement.add(0, actualElement);

		}

		actualElement = middleElement;
		for (int i=1;i<ipElement.size();i++){
			actualElement = getSuccesor(actualElement, ipElement);
			
			if (actualElement==null)
				return orderedIpElement;
			
			orderedIpElement.add(actualElement);

		}

		return orderedIpElement;		
	}
	
	private EList<InteractionProtocolElement> getRealIpElement() {
//		if (element == null) {
//			element = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT);
//		}
		
		EList<InteractionProtocolElement> realIpElement = new BasicEList<InteractionProtocolElement>();
		
		realIpElement.addAll(getInteractionPathBusinessMessages());
		realIpElement.addAll(this.getElement());
	
		return realIpElement;
	}
	
	private InteractionProtocolElement  getPredecesor(InteractionProtocolElement actualElement, EList<InteractionProtocolElement> ipElement){
		
		for (int i=0;i<ipElement.size();i++){
			if (ipElement.get(i).getSuccessor()==actualElement)
				return ipElement.get(i);
		}
		return null;
	}

	private InteractionProtocolElement  getSuccesor(InteractionProtocolElement actualElement, EList<InteractionProtocolElement> ipElement){
		
		for (int i=0;i<ipElement.size();i++){
			if (ipElement.get(i).getPredecessor()==actualElement)
				return ipElement.get(i);
		}
		return null;
	}

	public boolean interactionPathVerification(DiagnosticChain diagnostics,
			Map context) {
		// TODO: implement this method
		// -> specify the condition that violates the invariant
		// -> verify the details of the diagnostic, including severity and message
		// Ensure that you remove @generated or mark it @generated NOT
		if (validateInteractionPath()) {
			if (diagnostics != null) {
				diagnostics.add
					(new BasicDiagnostic
						(Diagnostic.ERROR,
						 InteractionprotocolsValidator.DIAGNOSTIC_SOURCE,
						 InteractionprotocolsValidator.EMPTY_INTERACTION_PATH,
						 EcorePlugin.INSTANCE.getString("_UI_GenericInvariant_diagnostic", new Object[] { "Interaction cannot be empty", EObjectValidator.getObjectLabel(this, context) }), //$NON-NLS-1$ //$NON-NLS-2$
						 new Object [] { this }));
			}
			return false;
		}
		return true;
	}
	
	private boolean validateInteractionPath(){
		boolean result = false;
	//	result = this.getElement().isEmpty() && this.getInteractionPathBusinessMessages().isEmpty();
		
	//	return result;
		
		return false;
	}
		
	

} //InteractionPathImpl
